package cn.kgc.springboot.service.impl;

import cn.kgc.springboot.common.CodeMsg;
import cn.kgc.springboot.entity.RolePer;
import cn.kgc.springboot.mapper.RolePerMapper;
import cn.kgc.springboot.util.LayTable;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.springboot.entity.Menu;
import cn.kgc.springboot.service.MenuService;
import cn.kgc.springboot.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 */
@Service
@Transactional
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService{

    @Autowired
    private  MenuMapper menuMapper;

    @Autowired
    private RolePerMapper  rolePerMapper;

    @Override
    public LayTable deleteAuth(Integer id) {

        // 根据权限id 查询是否有子菜单
        List<Menu> menus = menuMapper.selectList(new QueryWrapper<Menu>().eq("pid", id));
        if(ObjectUtils.isNotNull(menus)){  // 删除的权限包含子菜单  不能删除
            return  new LayTable().setMsg(CodeMsg.SUBMENU_EXIS.getMsg()).setCode(CodeMsg.SUBMENU_EXIS.getCode());
        }

        // 根据id  删除权限 信息
        menuMapper.deleteById(id);

        // 根据权限id   删除 角色权限关系表表中的数据
        rolePerMapper.delete(new QueryWrapper<RolePer>().eq("per_id", id));
        return new LayTable().setMsg(CodeMsg.SUCCESS.getMsg()).setCode(CodeMsg.SUCCESS.getCode());
    }
}




